package com.fy.market.strategy.service.rule.chain.impl;

import cn.hutool.core.util.RandomUtil;
import com.fy.market.strategy.model.entity.RuleActionEntity;
import com.fy.market.strategy.model.entity.RuleChainResultEntity;
import com.fy.market.strategy.repository.IStrategyRepository;
import com.fy.market.strategy.service.raffle.IRaffleStrategy;
import com.fy.market.strategy.service.rule.chain.AbstractLogicChain;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.security.SecureRandom;
import java.util.concurrent.ThreadLocalRandom;

/**
 * @author fwq
 * @date 2025-09-18
 * @desc 责任链默认节点
 */
@Slf4j
@Component("rule_default")
public class DefaultLogicChain extends AbstractLogicChain {

    @Resource
    private IStrategyRepository iStrategyRepository;

    /**
     * 默认节点
     * @param userId 用户ID
     * @param strategyId 策略ID
     * @return 奖品ID
     */
    @Override
    public RuleChainResultEntity logic(Integer userId, Integer strategyId) {

        Integer allRate = iStrategyRepository.queryAllRate(strategyId, 0);
        log.info("----- 开始执行默认过滤(前置) -----");
        Integer awardId = iStrategyRepository.doRaffle(strategyId, 0, RandomUtil.randomInt(1, allRate + 1));
        log.info("默认过滤接管,获得奖品:{}",awardId);

        return RuleChainResultEntity.builder().awardId(awardId).strategyId(strategyId).build();
    }
}
